package algorithm_learn.dataStructure;

public class Code_ReverseList {

    public static class Node{
        private int value;
        public Node next;

        public Node(int value) {
            this.value = value;
        }

    }

    public static class DoubleNode{
        private int value;
        public DoubleNode next;
        public DoubleNode last;

        public DoubleNode(int value) {
            this.value = value;
        }
    }
    /***
     * 链表头位移
     * @param head
     */
    public static void f(Node head){
        head = head.next;
    }

    /***
     * 反转单链表
     * @param head
     * @return 反转后的头结点
     */
    public static Node reverseLinkedList(Node head){
        Node pre = null;
        Node next = null;
        while (head != null){
            // 记下下一个头节点 防止丢失
            next = head.next;
            head.next = pre;
            pre = head;
            // 回归真实的head
            head = next;
        }
        return pre;
    }

    /***
     * 反转双链表 (1<===>2<===>3<===>4)
     * @param head 传入头节点！
     * @return 返回的是转换后的头节点
     */
    public static DoubleNode reversseDoubleList(DoubleNode head){
        DoubleNode pre = null;
        DoubleNode next = null;
        while (head != null){
            //记下后面的环境
            next = head.next;
            head.next = pre;
            head.last = next;
            pre = head;
            head = head.next;
        }
        return pre;
    }
    /***
     * 打印函数
     * @param node1 头节点
     */
    public static void print(Node node1){
        while(node1 != null){
            System.out.print(node1.value + "-->");
            node1 = node1.next;
        }
        System.out.println();
    }
    public static void main(String[] args) {
        Node node1 = new Node(1);
        node1.next = new Node(2);
        node1.next.next = new Node(3);
        //虽然产生了一个位移，但是是因为浅拷贝 她实际上的值并没有改变！ 是复制了一份新的放了进去
        f(node1);
        System.out.println("验证拷贝地址： "+node1.value);
        print(node1);

        node1 = reverseLinkedList(node1);

        print(node1);

    }
}
